import { MessageBox } from 'element-ui'
import i18n from '@/assets/i18n'
export function getDateMonth (date = new Date()) {
  if (!(date instanceof Date)) {
    date = new Date(date)
  }
  date.setFullYear(date.getFullYear(), date.getMonth(), 2)
  return date
}
export function formatDate (date = new Date(), format = 'yyyy-MM-dd') {
  date = date instanceof Date ? date : new Date(date)
  const year = date.getFullYear()
  const orginalMonth = date.getMonth() + 1
  const month = orginalMonth > 9 ? orginalMonth : '0' + orginalMonth
  const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
  let dateStr = ''
  switch (format) {
    case 'yyyy-MM-dd': dateStr = year + '-' + month + '-' + day; break
  }
  return dateStr
}
export function getSum (array, prop) {
  let sum = 0
  for (let i = 0, len = array.length; i < len; i++) {
    sum += parseFloat(array[i][prop])
  }
  return sum
}
export function messageBoxConfirm (type) {
  switch (type) {
    case 'delete':
      return deleteConfirm()
    case 'unsave':
      return unsaveConfirm()
  }
}
export function getAfter30DaysDate () {
  return Date.now() + 2592000000
}
export function greaterThanEqualsAfter30Days (time) {
  return time.getTime() >= Date.now() + 2592000000
}
export function greaterThanEqualsNow (time) {
  return time.getTime() >= Date.now()
}
function deleteConfirm () {
  return MessageBox.confirm(
    i18n.t('common.deleteTips'),
    i18n.t('common.tips'),
    {
      confirmButtonText: i18n.t('common.confirm'),
      cancelButtonText: i18n.t('common.cancel'),
      type: 'warning'
    }
  )
}
function unsaveConfirm () {
  return MessageBox.confirm(
    i18n.t('common.unsavedTips'),
    i18n.t('common.tips'),
    {
      confirmButtonText: i18n.t('common.confirm'),
      cancelButtonText: i18n.t('common.cancel'),
      type: 'warning'
    }
  )
}
